package com.edu.study.base.stuProductCatalog.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

import com.edu.study.base.stuProductCatalogTemplateData.domain.StuProductCatalogTemplateData;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.edu.study.base.stuProductCatalog.mapper.StuProductCatalogMapper;
import com.edu.study.base.stuProductCatalog.domain.StuProductCatalog;
import com.edu.study.base.stuProductCatalog.service.IStuProductCatalogService;

/**
 * 产品（含章节层级）Service业务层处理
 * 
 * @author zw
 * @date 2021-05-13
 */
@Service
//@DataSource(value = DataSourceType.SLAVE)
public class StuProductCatalogServiceImpl implements IStuProductCatalogService 
{
    @Autowired
    private StuProductCatalogMapper stuProductCatalogMapper;

    /**
     * 查询产品（含章节层级）
     * 
     * @param id 产品（含章节层级）ID
     * @return 产品（含章节层级）
     */
    @Override
    public StuProductCatalog selectStuProductCatalogById(String id)
    {
        return stuProductCatalogMapper.selectStuProductCatalogById(id);
    }

    /**
     * 查询产品（含章节层级）列表
     * 
     * @param stuProductCatalog 产品（含章节层级）
     * @return 产品（含章节层级）
     */
    @Override
    public List<StuProductCatalog> selectStuProductCatalogList(StuProductCatalog stuProductCatalog)
    {
        return stuProductCatalogMapper.selectStuProductCatalogList(stuProductCatalog);
    }

    @Override
    public List<StuProductCatalog> selectCourseList(StuProductCatalog stuProductCatalog) {
        return stuProductCatalogMapper.selectCourseList(stuProductCatalog);
    }

    @Override
    public List<StuProductCatalog> selectProductCatalogList(StuProductCatalog stuProductCatalog) {
        return stuProductCatalogMapper.selectProductCatalogList(stuProductCatalog);
    }

    @Override
    public List<StuProductCatalog> getProductCatalogChildrens(List<StuProductCatalog> pcList,StuProductCatalog c) {
        List<StuProductCatalog> returnList = new ArrayList<>();
        //一级目录数据
        List<StuProductCatalog> firstLevelList = pcList.stream().filter(t->c.getId().equals(t.getCourseParentId())).collect(Collectors.toList());

        for (Iterator<StuProductCatalog> iterator = firstLevelList.iterator(); iterator.hasNext();)
        {
            StuProductCatalog pc = (StuProductCatalog) iterator.next();
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (hasChild(pcList,pc))
            {
                recursionFn(pcList, pc);
            }
            returnList.add(pc);
        }
        if (returnList.isEmpty())
        {
            returnList = pcList;
        }
        return returnList;
    }

    /**
     * 递归列表
     */
    private void recursionFn(List<StuProductCatalog> list, StuProductCatalog t)
    {
        // 得到子节点列表
        List<StuProductCatalog> childList = getChildList(list, t);
        t.setChildren(childList);
        for (StuProductCatalog tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                recursionFn(list, tChild);
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private List<StuProductCatalog> getChildList(List<StuProductCatalog> list, StuProductCatalog t)
    {
        List<StuProductCatalog> tlist = new ArrayList<>();
        Iterator<StuProductCatalog> it = list.iterator();
        while (it.hasNext())
        {
            StuProductCatalog n = (StuProductCatalog) it.next();
            if (StringUtils.isNotNull(n.getCourseParentId()) && t.getId().equals(n.getCourseParentId()))
            {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<StuProductCatalog> list, StuProductCatalog t)
    {
        return getChildList(list, t).size() > 0 ? true : false;
    }


    /**
     * 新增产品（含章节层级）
     * 
     * @param stuProductCatalog 产品（含章节层级）
     * @return 结果
     */
    @Override
    public int insertStuProductCatalog(StuProductCatalog stuProductCatalog)
    {
        stuProductCatalog.setCreateTime(DateUtils.getNowDate());
        return stuProductCatalogMapper.insertStuProductCatalog(stuProductCatalog);
    }

    @Override
    public int insertStuProductCatalogList(List<StuProductCatalog> stuProductCatalogList) {
        return stuProductCatalogMapper.insertStuProductCatalogList(stuProductCatalogList);
    }

    /**
     * 修改产品（含章节层级）
     * 
     * @param stuProductCatalog 产品（含章节层级）
     * @return 结果
     */
    @Override
    public int updateStuProductCatalog(StuProductCatalog stuProductCatalog)
    {
        stuProductCatalog.setUpdateTime(DateUtils.getNowDate());
        return stuProductCatalogMapper.updateStuProductCatalog(stuProductCatalog);
    }

    /**
     * 批量删除产品（含章节层级）
     * 
     * @param ids 需要删除的产品（含章节层级）ID
     * @return 结果
     */
    @Override
    public int deleteStuProductCatalogByIds(String[] ids)
    {
        return stuProductCatalogMapper.deleteStuProductCatalogByIds(ids);
    }

    /**
     * 删除产品（含章节层级）信息
     * 
     * @param id 产品（含章节层级）ID
     * @return 结果
     */
    @Override
    public int deleteStuProductCatalogById(String id)
    {
        return stuProductCatalogMapper.deleteStuProductCatalogById(id);
    }
}
